package com.shop.loan.util;

import cn.hutool.core.date.DateUtil;
import com.shop.loan.mapper.BlacklistByMybatisMapper;
import com.shop.loan.mapper.EruptUserByMybatisMapper;
import com.shop.loan.model.entity.BlacklistByMybatis;
import com.shop.loan.model.entity.EruptUserByMybatis;
import com.shop.loan.model.enums.OpFailType;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class OpFailUtil {

    @Resource
    RedisUtil redisUtil;

    @Resource
    EruptUserByMybatisMapper eruptUserByMybatisMapper;

    @Resource
    BlacklistByMybatisMapper blacklistByMybatisMapper;

    @Value("${sys.loginFail}")
    private Integer loginFailNum;

    @Value("${sys.opPasswordFail}")
    private Integer opPasswordFail;

    /**
     * 操作失败过多 拉黑ip
     * @param requestIp
     * @param eruptUserId
     * @param opFailType
     */
    public void opFailOp(String requestIp, Long eruptUserId, OpFailType opFailType) {
        String reason = "";
        String redisKey = "";
        Integer setFailNum = 0;
        if (opFailType.getType().intValue() == OpFailType.login.getType()) {
            reason = "账号密码错误次数过多";
            redisKey = RedisCache.userLoginFailNumIp + requestIp + DateUtil.today();
            setFailNum = loginFailNum;
        } else if (opFailType.getType().intValue() == OpFailType.loan.getType()) {
            reason = "放款密码错误次数过多";
            redisKey = RedisCache.userOpFailNumIp + requestIp + DateUtil.today();
            setFailNum = opPasswordFail;
        } else if (opFailType.getType().intValue() == OpFailType.pay.getType()) {
            reason = "还款密码错误次数过多";
            redisKey = RedisCache.userOpFailNumIp + requestIp + DateUtil.today();
            setFailNum = opPasswordFail;
        }

        String failNumStr = redisUtil.get(redisKey);
        Integer failNum = 0;
        if (failNumStr != null) {
            failNum = Integer.valueOf(failNumStr);
        }
        failNum++;
        setFailNum = setFailNum == null ? 5 : setFailNum;
        if (failNum > setFailNum) {
            //账号密码错误5次  则拉黑ip  若管理员存在 冻结账号
            //拉黑ip
            BlacklistByMybatis blackList = new BlacklistByMybatis();
            blackList.setContent(requestIp);
            blackList.setCreateTime(new Date());
            blackList.setReason(reason);
            blacklistByMybatisMapper.insertSelective(blackList);
            //ip添加到缓存中
            redisUtil.hset(RedisCache.blackListByIp, requestIp, requestIp);

            //冻结账号（管理员）
            if (eruptUserId != null) {
                EruptUserByMybatis eruptUserByMybatis = new EruptUserByMybatis();
                eruptUserByMybatis.setStatus(false);
                eruptUserByMybatis.setId(eruptUserId);
                eruptUserByMybatisMapper.updateByPrimaryKeySelective(eruptUserByMybatis);
            }
        }
        redisUtil.setex(redisKey, 60 * 60 * 25, String.valueOf(failNum));
    }


}
